package SM.UI;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import SM.BusinessLogic.DBAccess;
import SM.BusinessLogic.DBService;
import SM.BusinessLogic.SessionVariables;
import SM.UI.Controls.DropDown;

/**
 * Servlet implementation class ReportIssue
 */

public class ReportIssue extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ReportIssue() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// doGet is used to render HTML content
	
		/*Variable Declaration*/
		
		PrintWriter out = response.getWriter();
		SM.UI.Controls.DropDown ddBuildingCodes = new DropDown("building_code", 80, "b_code");
		SM.UI.Controls.DropDown ddFlatCodes = new DropDown("flat_code", 80, "f_code");
		SM.UI.Controls.DropDown ddIssueCategory = new DropDown("issue_category", 350, "issue_cat");
		
		request.getRequestDispatcher("report-1.html").include(request, response);
		
		String user = SessionVariables.GetUser(request);
		
		if(user == null)
		{
			response.sendRedirect("../../index.html");
		}
		
		out.println("<table cellspacing=20>\n");
		out.println("<tr> \n" +
					"<td>User ID</td> \n" +
					"<td> <span id='name'>"  + user + "</span> </td>\n"+
				"</tr>\n");
		out.println("</table>\n");
		
		String DisplayString1 = "<table cellspacing=20><tr>" + 
				"<td class=\"col1\">Building Code</td>"+
				"<td class=\"col1\">";
		
				out.println(DisplayString1);
				
				// Building Codes
					synchronized(this) 
					{
						Connection connection = DBAccess.MakeConnection();
						CallableStatement statement = DBAccess.GetStatementForProcedureCall(connection, "BuildingCodesSelect()");
				
						ResultSet resultSet = DBService.ExecuteProcedure(connection, statement);
					
						ddBuildingCodes.SetDataSource(resultSet, 1, 2);
						ddBuildingCodes.DataBind();
						
						out.println(ddBuildingCodes.toString());
						
						DBAccess.CloseConnection(connection);
						DBAccess.CloseStatement(statement);
					}
				
				out.println("</td>"+
				"<td class=\"col2\">");
				
					synchronized(this) 
					{
						Connection connection = DBAccess.MakeConnection();
						CallableStatement statement = DBAccess.GetStatementForProcedureCall(connection, "FlatNumbersSelect()");
			
						ResultSet resultSet = DBService.ExecuteProcedure(connection, statement);
				
						ddFlatCodes.SetDataSource(resultSet, 1, 1);
						ddFlatCodes.DataBind();
					
						out.println(ddFlatCodes.toString());
						
						DBAccess.CloseConnection(connection);
						DBAccess.CloseStatement(statement);
					}
				
				
				String DisplayString2 = "</td>"+
				"</tr>"+
		"</table>"+

"<br class=\"clearfix\"/>"+

"<table width=\"100%\" cellspacing=20>"+
	"<tr>"+
		"<td colspan=2 style=\"text-align:center;\" class=\"head-t\">"+
			"<div style=\"text-slign:center;\">Issue Information</div>"+
		"</td>"+
	"</tr>"+
	"<tr>"+
		"<td class=\"col1\">Issue Category</td>"+
		"<td class=\"col2\">";
				
				out.println(DisplayString2);
				
			//<!-- Issue Category -->
				synchronized(this) 
				{
					Connection connection = DBAccess.MakeConnection();
					CallableStatement statement = DBAccess.GetStatementForProcedureCall(connection, "ResponsibilitiesSelect()");
		
					ResultSet resultSet = DBService.ExecuteProcedure(connection, statement);
			
					ddIssueCategory.SetDataSource(resultSet, 1, 2);
					ddIssueCategory.DataBind();
				
					out.println(ddIssueCategory.toString());
					
					DBAccess.CloseConnection(connection);
					DBAccess.CloseStatement(statement);
				}
		
				out.println("</td>"+
						"</tr>");
			
			
		request.getRequestDispatcher("report-2.html").include(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// doPost is used to Accept request from page and Make Changes
		
		
	}

}
